﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SW_SoftBully.Controllers;
using SW_SoftBully.Models.ModuloAdministracion.DepartamentoPeru;
using System.Data.SqlClient;
using log4net;

namespace SW_SoftBully.Models.ModuloAdministracion.DepartamentoPeru
{
    public class DepartamentoPeruDAO
    {
        public List<DepartamentoPeruBean> ListarDepartamentoPeru(DepartamentoPeruBean departamentoPeru)
        {
            try
            {
                List<DepartamentoPeruBean> listaDepartamentoPeru = new List<DepartamentoPeruBean>();

                String cadenaConfiguracion = ConfigurationManager.ConnectionStrings["SoftBully"].ConnectionString;

                SqlConnection sqlCon = new SqlConnection(cadenaConfiguracion);

                sqlCon.Open();

                string commandString = "SELECT * FROM Departamento_Lugar"; 

                SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);

                SqlDataReader dataReader = sqlCmd.ExecuteReader();

                while (dataReader.Read()){
                        DepartamentoPeruBean DepartamentoPeru = new DepartamentoPeruBean();
                        DepartamentoPeru.Nombre = Convert.ToString(dataReader["Nombre"]);
                        DepartamentoPeru.Id_Departamento = Convert.ToInt32(dataReader["Id_Departamento"]);
                        //System.Diagnostics.Debug.WriteLine("--------------------------------------_________MIERDA> " + DepartamentoPeru.Nombre);
                        listaDepartamentoPeru.Add(DepartamentoPeru);
                }
                dataReader.Close();
                sqlCon.Close();

                return listaDepartamentoPeru;
            }
            catch (Exception e)
            {
                String men = e.ToString();
                departamentoPeru.conexion = men;
                return null;
            }
        }

        public List<DepartamentoPeruBean> ListarDepartamentoProv(DepartamentoPeruBean departamento)
        {
            try
            {
                List<DepartamentoPeruBean> listaDepartamentoPeru = new List<DepartamentoPeruBean>();

                String cadenaConfiguracion = ConfigurationManager.ConnectionStrings["SoftBully"].ConnectionString;

                SqlConnection sqlCon = new SqlConnection(cadenaConfiguracion);

                sqlCon.Open();

                string commandString = "SELECT d.id_departamento, d.nombre FROM departamento_lugar d, edificio e where e.id_edificio = " + departamento.id_edificio +
                                       " AND d.id_departamento = e.id_departamento";

                System.Diagnostics.Debug.WriteLine("--------------------------------------_________MIERDA> " + commandString);

                SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);

                SqlDataReader dataReader = sqlCmd.ExecuteReader();

                while (dataReader.Read())
                {
                    DepartamentoPeruBean DepartamentoPeru = new DepartamentoPeruBean();
                    DepartamentoPeru.Nombre = Convert.ToString(dataReader["nombre"]);
                    DepartamentoPeru.Id_Departamento = Convert.ToInt32(dataReader["id_departamento"]);
                    //System.Diagnostics.Debug.WriteLine("--------------------------------------_________MIERDA> " + DepartamentoPeru.Nombre);
                    listaDepartamentoPeru.Add(DepartamentoPeru);
                }

                for (int i = 0; i < listaDepartamentoPeru.Count();i++)
                {
                    if (listaDepartamentoPeru[i].Id_Departamento != 15)
                    {
                        DepartamentoPeruBean DepPeru = new DepartamentoPeruBean();
                        DepPeru.Nombre = "LIMA";
                        DepPeru.Id_Departamento = 15;
                        listaDepartamentoPeru.Add(DepPeru);
                    }
                }

                dataReader.Close();
                sqlCon.Close();

                return listaDepartamentoPeru;
            }
            catch (Exception e)
            {
                String men = e.ToString();
                departamento.conexion = men;
                return null;
            }
        }
    }
}